﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-5-11 13:14:22
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL.TaoBao;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL.TaoBao
{
    /// <summary>
    /// 数据访问类 TB_XSJRD
    /// </summary>
    public class TB_XSJRD : ITB_XSJRD
    {
        public TB_XSJRD()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.TaoBao.TB_XSJRD model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                     new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@LSH", SqlDbType.Int),
                    new SqlParameter("@DJBH", SqlDbType.VarChar,20),
                    new SqlParameter("@RQ", SqlDbType.DateTime),
                    new SqlParameter("@TB_NO", SqlDbType.VarChar,20),
                    new SqlParameter("@YDJH", SqlDbType.VarChar,20),
                    new SqlParameter("@TD_ID", SqlDbType.Int),
                    new SqlParameter("@DY_ID", SqlDbType.Int),
                    new SqlParameter("@DYDM", SqlDbType.VarChar,50),
                    new SqlParameter("@DYMC", SqlDbType.VarChar,50),
                    new SqlParameter("@MJ", SqlDbType.VarChar,50),
                    new SqlParameter("@SL", SqlDbType.Int),
                    new SqlParameter("@SL_1", SqlDbType.Int),
                    new SqlParameter("@SL_2", SqlDbType.Int),
                    new SqlParameter("@SL_3", SqlDbType.Int),
                    new SqlParameter("@JE", SqlDbType.Decimal),
                    new SqlParameter("@JE_1", SqlDbType.Decimal),
                    new SqlParameter("@JE_2", SqlDbType.Decimal),
                    new SqlParameter("@JE_3", SqlDbType.Decimal),
                    new SqlParameter("@JE_4", SqlDbType.Decimal),
                    new SqlParameter("@ZDR", SqlDbType.VarChar,20),
                    new SqlParameter("@ZDRQ", SqlDbType.DateTime),
                    new SqlParameter("@FH", SqlDbType.Int),
                    new SqlParameter("@FHR", SqlDbType.VarChar,20),
                    new SqlParameter("@FHRQ", SqlDbType.DateTime),
                    new SqlParameter("@ZF", SqlDbType.Int),
                    new SqlParameter("@ZFR", SqlDbType.VarChar,20),
                    new SqlParameter("@ZFRQ", SqlDbType.DateTime),
                    new SqlParameter("@YS", SqlDbType.Int),
                    new SqlParameter("@YSR", SqlDbType.VarChar,20),
                    new SqlParameter("@YSRQ", SqlDbType.DateTime),
                    new SqlParameter("@BZ", SqlDbType.VarChar,255),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Direction = ParameterDirection.Output;

            parameters[0].Value = model.MID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;
            parameters[3].Value = model.LSH;

            if (model.DJBH != null)
                parameters[4].Value = model.DJBH;
            else
                parameters[4].Value = DBNull.Value;


            if (model.RQ != DateTime.MinValue)
                parameters[5].Value = model.RQ;
            else
                parameters[5].Value = DBNull.Value;


            if (model.TB_NO != null)
                parameters[6].Value = model.TB_NO;
            else
                parameters[6].Value = DBNull.Value;


            if (model.YDJH != null)
                parameters[7].Value = model.YDJH;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.TD_ID;
            parameters[9].Value = model.DY_ID;

            if (model.DYDM != null)
                parameters[10].Value = model.DYDM;
            else
                parameters[10].Value = DBNull.Value;


            if (model.DYMC != null)
                parameters[11].Value = model.DYMC;
            else
                parameters[11].Value = DBNull.Value;


            if (model.MJ != null)
                parameters[12].Value = model.MJ;
            else
                parameters[12].Value = DBNull.Value;

            parameters[13].Value = model.SL;
            parameters[14].Value = model.SL_1;
            parameters[15].Value = model.SL_2;
            parameters[16].Value = model.SL_3;
            parameters[17].Value = model.JE;
            parameters[18].Value = model.JE_1;
            parameters[19].Value = model.JE_2;
            parameters[20].Value = model.JE_3;
            parameters[21].Value = model.JE_4;

            if (model.ZDR != null)
                parameters[22].Value = model.ZDR;
            else
                parameters[22].Value = DBNull.Value;


            if (model.ZDRQ != DateTime.MinValue)
                parameters[23].Value = model.ZDRQ;
            else
                parameters[23].Value = DBNull.Value;

            parameters[24].Value = model.FH;

            if (model.FHR != null)
                parameters[25].Value = model.FHR;
            else
                parameters[25].Value = DBNull.Value;


            if (model.FHRQ != DateTime.MinValue)
                parameters[26].Value = model.FHRQ;
            else
                parameters[26].Value = DBNull.Value;

            parameters[27].Value = model.ZF;

            if (model.ZFR != null)
                parameters[28].Value = model.ZFR;
            else
                parameters[28].Value = DBNull.Value;


            if (model.ZFRQ != DateTime.MinValue)
                parameters[29].Value = model.ZFRQ;
            else
                parameters[29].Value = DBNull.Value;

            parameters[30].Value = model.YS;

            if (model.YSR != null)
                parameters[31].Value = model.YSR;
            else
                parameters[31].Value = DBNull.Value;


            if (model.YSRQ != DateTime.MinValue)
                parameters[32].Value = model.YSRQ;
            else
                parameters[32].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[33].Value = model.BZ;
            else
                parameters[33].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[34].Value = model.field1;
            else
                parameters[34].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[35].Value = model.field2;
            else
                parameters[35].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_ADD", parameters, out rowsAffected);

            if (rowsAffected > 0)
                return (int)parameters[0].Value;
            else
                return 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaoBao.TB_XSJRD model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@LSH", SqlDbType.Int),
                    new SqlParameter("@DJBH", SqlDbType.VarChar,20),
                    new SqlParameter("@RQ", SqlDbType.DateTime),
                    new SqlParameter("@TB_NO", SqlDbType.VarChar,20),
                    new SqlParameter("@YDJH", SqlDbType.VarChar,20),
                    new SqlParameter("@TD_ID", SqlDbType.Int),
                    new SqlParameter("@DY_ID", SqlDbType.Int),
                    new SqlParameter("@DYDM", SqlDbType.VarChar,50),
                    new SqlParameter("@DYMC", SqlDbType.VarChar,50),
                    new SqlParameter("@MJ", SqlDbType.VarChar,50),
                    new SqlParameter("@SL", SqlDbType.Int),
                    new SqlParameter("@SL_1", SqlDbType.Int),
                    new SqlParameter("@SL_2", SqlDbType.Int),
                    new SqlParameter("@SL_3", SqlDbType.Int),
                    new SqlParameter("@JE", SqlDbType.Decimal),
                    new SqlParameter("@JE_1", SqlDbType.Decimal),
                    new SqlParameter("@JE_2", SqlDbType.Decimal),
                    new SqlParameter("@JE_3", SqlDbType.Decimal),
                    new SqlParameter("@JE_4", SqlDbType.Decimal),
                    new SqlParameter("@ZDR", SqlDbType.VarChar,20),
                    new SqlParameter("@ZDRQ", SqlDbType.DateTime),
                    new SqlParameter("@FH", SqlDbType.Int),
                    new SqlParameter("@FHR", SqlDbType.VarChar,20),
                    new SqlParameter("@FHRQ", SqlDbType.DateTime),
                    new SqlParameter("@ZF", SqlDbType.Int),
                    new SqlParameter("@ZFR", SqlDbType.VarChar,20),
                    new SqlParameter("@ZFRQ", SqlDbType.DateTime),
                    new SqlParameter("@YS", SqlDbType.Int),
                    new SqlParameter("@YSR", SqlDbType.VarChar,20),
                    new SqlParameter("@YSRQ", SqlDbType.DateTime),
                    new SqlParameter("@BZ", SqlDbType.VarChar,255),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Value = model.MID;
            parameters[1].Value = model.PID;
            parameters[2].Value = model.TN_ID;
            parameters[3].Value = model.LSH;

            if (model.DJBH != null)
                parameters[4].Value = model.DJBH;
            else
                parameters[4].Value = DBNull.Value;


            if (model.RQ != DateTime.MinValue)
                parameters[5].Value = model.RQ;
            else
                parameters[5].Value = DBNull.Value;


            if (model.TB_NO != null)
                parameters[6].Value = model.TB_NO;
            else
                parameters[6].Value = DBNull.Value;


            if (model.YDJH != null)
                parameters[7].Value = model.YDJH;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.TD_ID;
            parameters[9].Value = model.DY_ID;

            if (model.DYDM != null)
                parameters[10].Value = model.DYDM;
            else
                parameters[10].Value = DBNull.Value;


            if (model.DYMC != null)
                parameters[11].Value = model.DYMC;
            else
                parameters[11].Value = DBNull.Value;


            if (model.MJ != null)
                parameters[12].Value = model.MJ;
            else
                parameters[12].Value = DBNull.Value;

            parameters[13].Value = model.SL;
            parameters[14].Value = model.SL_1;
            parameters[15].Value = model.SL_2;
            parameters[16].Value = model.SL_3;
            parameters[17].Value = model.JE;
            parameters[18].Value = model.JE_1;
            parameters[19].Value = model.JE_2;
            parameters[20].Value = model.JE_3;
            parameters[21].Value = model.JE_4;

            if (model.ZDR != null)
                parameters[22].Value = model.ZDR;
            else
                parameters[22].Value = DBNull.Value;


            if (model.ZDRQ != DateTime.MinValue)
                parameters[23].Value = model.ZDRQ;
            else
                parameters[23].Value = DBNull.Value;

            parameters[24].Value = model.FH;

            if (model.FHR != null)
                parameters[25].Value = model.FHR;
            else
                parameters[25].Value = DBNull.Value;


            if (model.FHRQ != DateTime.MinValue)
                parameters[26].Value = model.FHRQ;
            else
                parameters[26].Value = DBNull.Value;

            parameters[27].Value = model.ZF;

            if (model.ZFR != null)
                parameters[28].Value = model.ZFR;
            else
                parameters[28].Value = DBNull.Value;


            if (model.ZFRQ != DateTime.MinValue)
                parameters[29].Value = model.ZFRQ;
            else
                parameters[29].Value = DBNull.Value;

            parameters[30].Value = model.YS;

            if (model.YSR != null)
                parameters[31].Value = model.YSR;
            else
                parameters[31].Value = DBNull.Value;


            if (model.YSRQ != DateTime.MinValue)
                parameters[32].Value = model.YSRQ;
            else
                parameters[32].Value = DBNull.Value;


            if (model.BZ != null)
                parameters[33].Value = model.BZ;
            else
                parameters[33].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[34].Value = model.field1;
            else
                parameters[34].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[35].Value = model.field2;
            else
                parameters[35].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            int rowsAffected;
            SqlParameter[] parameters ={ };
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_GetMaxId", parameters, out rowsAffected);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaoBao.TB_XSJRD GetModel(int MID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_XSJRD> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_XSJRD_GetList", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_XSJRD> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[TB_XSJRD]", null, pageSize, pageIndex, fldSort, sort, strCondition, "MID", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TaoBao.TB_XSJRD GetModel(DataRow r)
        {
            Model.TaoBao.TB_XSJRD model = new Model.TaoBao.TB_XSJRD();
            model.MID = SqlHelper.GetInt(r["MID"]);
            model.PID = SqlHelper.GetInt(r["PID"]);
            model.TN_ID = SqlHelper.GetInt(r["TN_ID"]);
            model.LSH = SqlHelper.GetInt(r["LSH"]);
            model.DJBH = SqlHelper.GetString(r["DJBH"]);
            model.RQ = SqlHelper.GetDateTime(r["RQ"]);
            model.TB_NO = SqlHelper.GetString(r["TB_NO"]);
            model.YDJH = SqlHelper.GetString(r["YDJH"]);
            model.TD_ID = SqlHelper.GetInt(r["TD_ID"]);
            model.DY_ID = SqlHelper.GetInt(r["DY_ID"]);
            model.DYDM = SqlHelper.GetString(r["DYDM"]);
            model.DYMC = SqlHelper.GetString(r["DYMC"]);
            model.MJ = SqlHelper.GetString(r["MJ"]);
            model.SL = SqlHelper.GetInt(r["SL"]);
            model.SL_1 = SqlHelper.GetInt(r["SL_1"]);
            model.SL_2 = SqlHelper.GetInt(r["SL_2"]);
            model.SL_3 = SqlHelper.GetInt(r["SL_3"]);
            model.JE = SqlHelper.GetDecimal(r["JE"]);
            model.JE_1 = SqlHelper.GetDecimal(r["JE_1"]);
            model.JE_2 = SqlHelper.GetDecimal(r["JE_2"]);
            model.JE_3 = SqlHelper.GetDecimal(r["JE_3"]);
            model.JE_4 = SqlHelper.GetDecimal(r["JE_4"]);
            model.ZDR = SqlHelper.GetString(r["ZDR"]);
            model.ZDRQ = SqlHelper.GetDateTime(r["ZDRQ"]);
            model.FH = SqlHelper.GetInt(r["FH"]);
            model.FHR = SqlHelper.GetString(r["FHR"]);
            model.FHRQ = SqlHelper.GetDateTime(r["FHRQ"]);
            model.ZF = SqlHelper.GetInt(r["ZF"]);
            model.ZFR = SqlHelper.GetString(r["ZFR"]);
            model.ZFRQ = SqlHelper.GetDateTime(r["ZFRQ"]);
            model.YS = SqlHelper.GetInt(r["YS"]);
            model.YSR = SqlHelper.GetString(r["YSR"]);
            model.YSRQ = SqlHelper.GetDateTime(r["YSRQ"]);
            model.BZ = SqlHelper.GetString(r["BZ"]);
            model.field1 = SqlHelper.GetString(r["field1"]);
            model.field2 = SqlHelper.GetString(r["field2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.TaoBao.TB_XSJRD> GetList(DataSet ds)
        {
            List<Model.TaoBao.TB_XSJRD> l = new List<Model.TaoBao.TB_XSJRD>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
